﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MessageWorkflow.Handler
{
	public class DynamicWorkflowHandler<T> : WorkflowHandler<T>
	{      
        public DynamicWorkflowHandler(bool handleDerivedTypes, Func<AsyncCallback, T, IAsyncResult> method)
            : base(handleDerivedTypes)
        {
            if (method == null)
                throw new ArgumentNullException("method");

            this.Method = method;
        }

        protected override IAsyncResult BeginHandleMessage(AsyncCallback callback, T message)
        {
            return Method(callback, message);
        }

        protected override void EndHandleMessage(IAsyncResult operation)
        {
            base.EndHandleMessage(operation);
        }

        protected Func<AsyncCallback, T, IAsyncResult> Method { get; set; }

        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return false;
            }
            else
            {
                DynamicWorkflowHandler<T> otherHandler = obj as DynamicWorkflowHandler<T>;
                if (otherHandler == null)
                {
                    return false;
                }
                else
                {
                    return this.Method.Equals(otherHandler.Method);
                }
            }
        }

        public override int GetHashCode()
        {
            return Method.GetHashCode();
        }
    }
}
